/*============================================================================*/
/*                   CONTINENTAL AUTOMOTIVE SYSTEMS                           */
/*============================================================================*/
/*                        OBJECT SPECIFICATION                                */
/*============================================================================*
* name:            MALPIT_Init.c
* version:         1.1
* created_by:      Lizette Acosta/Rodrigo Casados
* date_created:    June 22 2013
*=============================================================================*/
/* DESCRIPTION : Initializes ports                                            */
/*============================================================================*/
/* FUNCTION COMMENT : Initializes PIT                                         */
/* application stuff.                                                         */
/*                                                                            */
/*============================================================================*/
/*  REVISION |   DATE      |                               |      AUTHOR      */
/*----------------------------------------------------------------------------*/
/*  1.1      | 22/June/2013 |                              | Lizette Acosta   */
/*                                                           Rodrigo Casados  */
/*============================================================================*/

/*****************************************************************************************************
* Include files
*****************************************************************************************************/

#include "stdtypedef.h"
#include "typedefs.h"
#include "IntcInterrupts.h"
#include "MALPIT.h"

/*****************************************************************************************************
* Definition of  VARIABLEs - 
*****************************************************************************************************/

/* PIT_Callback, function to be called upon timeout of PIT channel N */
tCallbackFunction PIT_Callback[2];

/*****************************************************************************************************
* Definition of module wide (CONST-) CONSTANTs 
*****************************************************************************************************/

/*****************************************************************************************************
* Code of module wide FUNCTIONS
*****************************************************************************************************/

/****************************************************************************************************/
/**
* \brief    PIT device - Low level initialization
* \author   Abraham Tezmol
* \param    void
* \return   void
*/
void PIT_device_init(void) 
{
    PIT.PITMCR.R = 0x00000001;       /* Enable PIT and configure timers to stop in debug mode */
}

/****************************************************************************************************/
/**
* \brief    Install Interrupt routine for channels 0 and 1
* \author   Lizette Acosta
* \param    void
* \return   void
*/
void PIT_InstallInt(void)
{
    /* Install Interrupt routine for specific channels */
    INTC_InstallINTCInterruptHandler(PIT_channel_0_isr,59,5);	/* Interrupt for PIT, Channel 0 */
    															/* Vector: 59 */
    INTC_InstallINTCInterruptHandler(PIT_channel_1_isr,60,5);	/* Interrupt for PIT, Channel 1 */
    															/* Vector: 60 */    															
}


/****************************************************************************************************/
/**
* \brief    PIT Channel Low level configuration
* \author   Abraham Tezmol
* \param    uint8_t channel - Channel to be configured
* \param    tCallbackFunction Callback - Function to invoke upon PIT count to zero event
* \return   void
*/
void PIT_channel_configure(uint8_t channel, tCallbackFunction Callback) 
{
	T_UWORD luw_PITChannelValue[2] = { PIT_CHANNEL_1_VALUE, PIT_CHANNEL_2_VALUE};
	
    PIT.CH[channel].LDVAL.R = luw_PITChannelValue[channel];      /* PIT1 timeout --> Refer to PIT.h file for calculations */
    PIT_Callback[channel]   = Callback;               /* Initialize callback function */

}

/****************************************************************************************************/
/**
* \brief    PIT channel Low level Start procedure
* \author   Abraham Tezmol
* \param    uint8_t channel - PIT channel to start
* \return   void
*/
void PIT_channel_start(uint8_t channel)
{
    PIT.CH[channel].TCTRL.R = 0x000000003;            /* Enable PIT1 interrupt and make PIT active to count */
}


/****************************************************************************************************/
/**
* \brief    PIT channel Low level Stop procedure
* \author   Abraham Tezmol
* \param    uint8_t channel - PIT channel to stop
* \return   void
*/
void PIT_channel_stop(uint8_t channel)
{
	PIT.CH[channel].TCTRL.R = 0x000000000;            /* Disable PIT channel interrupt and make PIT inactive to count */
}



/***************************************************************************************************/
/**
* \author   Lizette Acosta
* \param    void
* \return   void
/***************************************************************************************************/
/* Description: PIT_channel_0_isr Is intended for debouncing purpouses only; it gets called "OnDemand"
/* by u8fn_Deboouncer (calls PIT_channel_start), and stopped by itself (calls PIT_channel_stop)
/* upon debouncing's time lapse completion (10ms), and calls back u8fn_Deboouncer.
/* Therefore, PIT_Callback[PIT_CALLBACK_1] shall be initialized with the corresponding debouncer func.
/***************************************************************************************************/

void PIT_channel_0_isr(void)		/* 10 ms PIT */
{
  if (PIT.CH[PIT_CHANNEL_0_10ms].TFLG.B.TIF)
  {
    /* Clear the time-base interrupt flag */
    PIT.CH[PIT_CHANNEL_0_10ms].TFLG.B.TIF = 1;
    
    /* Set Debouncer's flag, which indicates that 10ms time lapse is completed */
	gub_DebouncerFlag = TRUE;
	
	/* Disable PIT channel interrupt and make PIT inactive to count */
    PIT_channel_stop(PIT_CHANNEL_0_10ms);
    
	if( PIT_Callback[PIT_CALLBACK_1] != NULL )
  	{
  		PIT_Callback[PIT_CALLBACK_1](); /* Calls back Debouncer Function */
  	} 
  }
    
  else
  {
  }
  
}



/***************************************************************************************************/
/**
* \author   Lizette Acosta
* \param    void
* \return   void
/***************************************************************************************************/
/* Description: 100ms PIT interruption
/***************************************************************************************************/
void PIT_channel_1_isr(void)
{
  if (PIT.CH[PIT_CHANNEL_1_100ms].TFLG.B.TIF)
  {
    /* Clear the time-base interrupt flag */
    PIT.CH[PIT_CHANNEL_1_100ms].TFLG.B.TIF = 1;
    
    /*call callback function, if initialized*/
        if( PIT_Callback[PIT_CALLBACK_1] != NULL )
        {
            PIT_Callback[PIT_CALLBACK_1]();
        }
        
        else
        {
        }
  }
  
  else
  {
  }
}
